<?php
/**
 * @file search.php
 * @author LJ (liangjian@haibao.com)
 * @date 2015年12月18日 上午11:32:14
 * @brief 搜索
 */
namespace haibao\app\business;

class Search extends \haibao\app\business\Base {

    /**
     * @var $_instance
     */
    private static $_instance;
    
    /**
     * instance()
     */
    private static function instance(){
        if(!self::$_instance){
            self::$_instance = new \haibao\app\data\Search();
        }
        return self::$_instance;
    }
    
    /**
     * 推荐明星列表
     */
    public static function getHotStarList($limit = 5){
        if(!$retStarList = (array)self::instance()->getHotStarListFromCache($limit)){
            $starIdArr = self::getStarIdArr($limit);
            $starList = self::instance()->getStarByIds($starIdArr);
            if($starList){
                foreach($starList as $key=>$star){
                    array_push($retStarList, array(
                        'starId' => $star['Id'],
                        'starName' => $star['NameCn'],
                        'starNameEn' => $star['NameEn'],
                    ));
                }
            }
            self::instance()->setHotStarListCache($limit, $retStarList);
        }
        
        return $retStarList;
    }
    
    /**
     * 自动提示明星列表
     */
    public static function autoList($keyword){
        if(!$keyword){
            throw new BusinessException('参数不正确', self::DATA_ERROR_CODE_EMPTY);
        }
        return self::instance()->autoList($keyword);
    }
    
    /**
     * 搜索结果列表
     */
    public static function searchArticle($keyword, $page){
        if(!$keyword){
            throw new BusinessException('参数不正确', self::DATA_ERROR_CODE_EMPTY);
        }
        return self::instance()->searchArticle($keyword, $page);
    }
    
    private static function getStarIdArr($limit){
        $datetime = date('Y-m-d H:i:s', strtotime('-1 week'));
        $recordList = self::instance()->getLeastArticleStar($datetime);
        if(empty($recordList)){
            $datetime = date('Y-m-d H:i:s', strtotime('-1 month'));
            $recordList = self::instance()->getLeastArticleStar($datetime);
        }
        if(count($recordList) < $limit){
            $datetime = date('Y-m-d H:i:s', strtotime('-2 month'));
            $recordList = self::instance()->getLeastArticleStar($datetime);
        }
        if(count($recordList) < $limit){
            $datetime = date('Y-m-d H:i:s', strtotime('-1 year'));
            $recordList = self::instance()->getLeastArticleStar($datetime);
        }
        if($recordList){
            foreach($recordList as $key=>$list){
                $starId = $list['StarId'];
                $radix = 0;
                $radix += $list['ClickNum'] * 0.01;
                $radix += $list['CommentNum'] * 0.08;
                $radix += $list['VoteNum'] * 0.02;
                $radix += $list['ShareNum'] * 0.08;
                $radix += $list['PraiseNum'] * 0.05;
                $radix += $list['AppClickNum'] * 0.01;
                $radix += $list['PcClickNum'] * 0.01;
                $radix += $list['WapClickNum'] * 0.01;
                $radix += $list['cnt'] * 0.1;
                unset($recordList[$key]);
                $recordList[$key]['StarId'] = $starId;
                $recordList[$key]['Radix'] = $radix;
            }
        }
        $recordList = self::arraySort($recordList, 'Radix');
        $recordList = array_slice($recordList, 0, $limit);
        
        $starIdArr = array();
        if($recordList){
            foreach($recordList as $item){
                array_push($starIdArr, $item['StarId']);
            }
        }
        return $starIdArr;
    }
    
    /**
     * 二维数组按照某以键值排序
     * arraySort($array, $keys, $type = SORT_DESC)
     */
    private static function arraySort($array, $keys, $type = SORT_DESC) {
        $keysValue = $newArray = array();
        foreach ($array as $k => $v) {
            $keysValue[$k] = $v[$keys];
        }
        if ($type == SORT_ASC) {
            asort($keysValue);
        } else {
            arsort($keysValue);
        }
        reset($keysValue);
        foreach ($keysValue as $k => $v) {
            array_push($newArray, $array[$k]);
        }
        return $newArray;
    }
}